home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / (A)P / (A)P1.ADF / sparks / sparks.c < prev   
C/C++ Source or Header  |  1987-05-25  |  7KB  |  276 lines

  1. /************************************************************************
  2.  * Sparks.c -- One of those graphics dazzlers which are so easy to do on
  3.  *            the Amiga.  Aside from that, I think it is a fairly decent
  4.  *            example of how to do simple things - menus, for example,
  5.  *            using Intuition.  Hope ya'all enjoy it.
  6.  *
  7.  *            Copyright (c) 1985 by Scott Ballantyne
  8.  *            Mainly so I can say give it away, distribute it, post it
  9.  *            on other nets, etc.
  10.  ************************************************************************/
  11.  
  12. #include <exec/types.h> /* Not all of these are used -- I include them */
  13. #include <exec/nodes.h> /* 'cause I'm sick of compiler warnings.       */
  14. #include <exec/lists.h>
  15. #include <exec/exec.h>
  16. #include <exec/execbase.h>
  17. #include <exec/ports.h>
  18. #include <exec/devices.h>
  19. #include <exec/memory.h>
  20. #include <hardware/blit.h>
  21. #include <graphics/copper.h>
  22. #include <graphics/regions.h>
  23. #include <graphics/rastport.h>
  24. #include <graphics/gfxbase.h>
  25. #include <graphics/gfxmacros.h>
  26. #include <graphics/gels.h>
  27. #include <intuition/intuition.h>
  28.  
  29. struct IntuitionBase *IntuitionBase = NULL;
  30. struct GfxBase *GfxBase = NULL;
  31.  
  32.  
  33. #define MAXX   640
  34.  
  35. struct NewScreen MyScreen =
  36. { 0,0,MAXX,200,4,0,1,HIRES, CUSTOMSCREEN, NULL, "Sparks!", 0,0,};
  37.  
  38.    struct NewWindow DrawWindow = {
  39.       0,0,MAXX,200,
  40.       0,1,
  41.       MENUPICK,
  42.       BORDERLESS | BACKDROP | ACTIVATE,
  43.       NULL,
  44.       NULL,
  45.       NULL,
  46.       NULL,
  47.       NULL,
  48.       0,0,0,0,
  49.       CUSTOMSCREEN,
  50.    };
  51.  
  52. struct Screen *Screen = NULL;
  53. struct Window *Backdrop = NULL;
  54. struct RastPort *DrawRP;
  55. struct ViewPort *DrawVP;
  56. struct IntuiMessage  *message;
  57.  
  58. struct MenuItem OnlyMenuItems[3];
  59. struct IntuiText OnlyMenuText[3];
  60. struct Menu OnlyMenu[1];
  61.  
  62. #define MAXLINES  125
  63. #define ERASE     0
  64. main()
  65. {
  66.    int lx1[MAXLINES], lx2[MAXLINES], x1, x2;
  67.    UBYTE ly1[MAXLINES], ly2[MAXLINES], y1, y2;
  68.    UBYTE  cl;
  69.    BYTE color;
  70.    int deltax1, deltay1, deltax2, deltay2;
  71.    ULONG class;
  72.    USHORT code, ItemNum;
  73.  
  74.    for(x1 = 0; x1 < MAXLINES; x1++)
  75.       lx1[x1] = ly1[x1] = lx2[x1] = ly2[x1] = 0;
  76.  
  77.    color = 2;
  78.  
  79.    x1 = x2 = 160;
  80.    y1 = y2 = 100;
  81.    cl = 0;
  82.    selectdelta(&deltax1, &deltay1, &deltax2, &deltay2);
  83.  
  84.    if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0)))
  85.          exit(1);
  86.  
  87.    if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0))) {
  88.       cleanitup();
  89.       exit(2);
  90.    }
  91.  
  92.    if(!(Screen = (struct Screen *)OpenScreen(&MyScreen))) {
  93.       cleanitup();
  94.       exit(3);
  95.    }
  96.  
  97.    DrawWindow.Screen = Screen;
  98.  
  99.    if(!(Backdrop = (struct Window *)OpenWindow(&DrawWindow))) {
  100.       cleanitup();
  101.       exit(4);
  102.    }
  103.  
  104.  
  105.    DrawRP = Backdrop->RPort;     /* Draw into backdrop window */
  106.    DrawVP = &Screen->ViewPort;   /* Set colors in Screens VP  */
  107.    setcolors();
  108.  
  109.    initmenuitems();
  110.    initmenu();
  111.    SetMenuStrip(Backdrop, &OnlyMenu[0]);
  112.  
  113.    FOREVER {
  114.       while(message = (struct IntuiMessage *)GetMsg(Backdrop->UserPort)) {
  115.          class = message->Class;
  116.          code = message->Code;
  117.          ReplyMsg(message);
  118.  
  119.          if (class == MENUPICK && code != MENUNULL) {
  120.             ItemNum = ITEMNUM( code );
  121.             switch (ItemNum) {
  122.                case 0:
  123.                   ShowTitle(Screen, FALSE);
  124.                   break;
  125.                case 1:
  126.                   ShowTitle(Screen, TRUE);
  127.                   break;
  128.                case 2:
  129.                   ClearMenuStrip(Backdrop);
  130.                   cleanitup();
  131.                   exit(0);
  132.             }
  133.          }
  134.       }
  135.  
  136.       draw(lx1[cl], ly1[cl], lx2[cl], ly2[cl], ERASE);
  137.       if (RangeRand(6) == 0)
  138.          color = RangeRand(16);
  139.       if (RangeRand(6) == 0)
  140.          selectdelta(&deltax1, &deltay1, &deltax2, &deltay2);
  141.       adjust_x(&x1, &deltax1);
  142.       adjust_y(&y1, &deltay1);
  143.       adjust_x(&x2, &deltax2);
  144.       adjust_y(&y2, &deltay2);
  145.       draw(x1, y1, x2, y2, color);
  146.       lx1[cl] = x1;
  147.       lx2[cl] = x2;
  148.       ly1[cl] = y1;
  149.       ly2[cl] = y2;
  150.       if (++cl >= MAXLINES)
  151.          cl = 0;
  152.    }
  153. }
  154.  
  155. cleanitup()    /* release allocated resources */
  156. {
  157.    if (Backdrop)
  158.       CloseWindow(Backdrop);
  159.    if (Screen)
  160.       CloseScreen(Screen);
  161.    if (GfxBase)
  162.       CloseLibrary(GfxBase);
  163.    if (IntuitionBase)
  164.       CloseLibrary(IntuitionBase);
  165. }
  166.  
  167. adjust_x(x, deltax)  /* make new xcor - if out of range adjust */
  168. int *x, *deltax;
  169. {
  170.    int junk;
  171.  
  172.    junk = *x + *deltax;
  173.    if (junk < 1 | junk >= MAXX) {
  174.       junk = *x;
  175.       *deltax *= -1;
  176.    }
  177.    *x = junk;
  178. }
  179.  
  180. adjust_y(y, deltay)
  181. UBYTE  *y;
  182. int  *deltay;
  183. {
  184.    int junk;
  185.  
  186.    junk = *y + *deltay;
  187.    if (junk < 1 | junk > 199) {
  188.       junk = *y;
  189.       *deltay *= -1;
  190.    }
  191.    *y = junk;
  192. }
  193.  
  194. selectdelta(dx1, dy1, dx2, dy2)
  195. int *dx1, *dy1, *dx2, *dy2;
  196. {
  197.    *dx1 = 2 * (RangeRand(7) - 3);
  198.    *dy1 = 2 * (RangeRand(7) - 3);
  199.    *dx2 = 2 * (RangeRand(7) - 3);
  200.    *dy2 = 2 * (RangeRand(7) - 3);
  201. }
  202.  
  203. draw(x1, y1, x2, y2, color)
  204. int x1, x2;
  205. UBYTE y1, y2;
  206. BYTE color;
  207. {
  208.    SetAPen(DrawRP, color);
  209.    SetDrMd(DrawRP, JAM1);
  210.    Move(DrawRP, x1, y1);
  211.    Draw(DrawRP, x2, y2);
  212. }
  213.  
  214. setcolors()
  215. {
  216.    SetRGB4(DrawVP, 0, 0, 0, 0);
  217.    SetRGB4(DrawVP, 1, 3, 5, 10);
  218.    SetRGB4(DrawVP, 2, 15, 15, 15);
  219.    SetRGB4(DrawVP, 3, 15, 6, 0);
  220.    SetRGB4(DrawVP, 4, 14, 3, 0);
  221.    SetRGB4(DrawVP, 5, 15, 11, 0);
  222.    SetRGB4(DrawVP, 6, 15, 15, 2);
  223.    SetRGB4(DrawVP, 7, 11, 15, 0);
  224.    SetRGB4(DrawVP, 8, 5, 13, 0);
  225.    SetRGB4(DrawVP, 9, 0, 0, 15);
  226.    SetRGB4(DrawVP, 10, 3, 6, 15);
  227.    SetRGB4(DrawVP, 11, 7, 7, 15);
  228.    SetRGB4(DrawVP, 12, 12, 0, 14);
  229.    SetRGB4(DrawVP, 13, 15, 2, 14);
  230.    SetRGB4(DrawVP, 15, 13, 11, 8);
  231. }
  232.  
  233. initmenuitems()
  234. {
  235.    short n;
  236.  
  237.    for(n = 0; n < 3; n++) {  /* One struct for each item */
  238.       OnlyMenuItems[n].NextItem = &OnlyMenuItems[n + 1]; /* next item */
  239.       OnlyMenuItems[n].LeftEdge = 0;
  240.       OnlyMenuItems[n].TopEdge = 10 * n;
  241.       OnlyMenuItems[n].Width = 112;
  242.       OnlyMenuItems[n].Height = 10;
  243.       OnlyMenuItems[n].Flags = ITEMTEXT | ITEMENABLED | HIGHCOMP;
  244.       OnlyMenuItems[n].MutualExclude = 0;
  245.       OnlyMenuItems[n].ItemFill = (APTR)&OnlyMenuText[n];
  246.       OnlyMenuItems[n].SelectFill = NULL;
  247.       OnlyMenuItems[n].Command = 0;
  248.       OnlyMenuItems[n].SubItem = NULL;
  249.       OnlyMenuItems[n].NextSelect = 0;
  250.  
  251.       OnlyMenuText[n].FrontPen = 0;
  252.       OnlyMenuText[n].BackPen = 1;
  253.       OnlyMenuText[n].DrawMode = JAM2;
  254.       OnlyMenuText[n].LeftEdge = 0;
  255.       OnlyMenuText[n].TopEdge = 1;
  256.       OnlyMenuText[n].ITextFont = NULL;
  257.       OnlyMenuText[n].NextText = NULL;
  258.    }
  259.    OnlyMenuItems[2].NextItem = NULL; /* Last item */
  260.  
  261.    OnlyMenuText[0].IText = (UBYTE *)"Hide Title Bar";
  262.    OnlyMenuText[1].IText = (UBYTE *)"Show Title Bar";
  263.    OnlyMenuText[2].IText = (UBYTE *)"QUIT!";
  264. }
  265. initmenu()
  266. {
  267.    OnlyMenu[0].NextMenu = NULL;                 /* No more menus */
  268.    OnlyMenu[0].LeftEdge = 0;
  269.    OnlyMenu[0].TopEdge = 0;
  270.    OnlyMenu[0].Width = 85;
  271.    OnlyMenu[0].Height = 10;
  272.    OnlyMenu[0].Flags = MENUENABLED;             /* All items selectable */
  273.    OnlyMenu[0].MenuName = "Actions";
  274.    OnlyMenu[0].FirstItem = &OnlyMenuItems[0];   /* Pointer to first item */
  275. }
  276.